//
//  XCMainViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/7.
//  Copyright © 2016年 梁新昌. All rights reserved.
//
#import "SONOfHeaderFile.h"

@interface XCMainViewController ()<UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, XCPlayingDelegate, XCHotListTableViewDelegate, XCSingleBottumViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) XCFirstDataModel *firstDataModel;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) UITableView *findTableView;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) AutoScrollView *autoScrollView;
@property (nonatomic, retain) NSMutableArray *picArray;
@property (nonatomic, copy) NSMutableString *senderPicUrl;
@property (nonatomic, retain) UIImageView *myHeadImageView;
@property (nonatomic, retain) UIImagePickerController *imagePicker;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *songlistNameMutableArray;
@property (nonatomic, retain) NSMutableArray *tuiJianDataSource;
@property (nonatomic, retain) NSMutableArray *mutableArraySongName;
@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;
@property (nonatomic, retain) NSMutableArray *nameArray;
//@property (nonatomic, assign) NSInteger songListIndex;
@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;
@property (nonatomic, copy) NSString *libraryPath;
@property (nonatomic, retain) NSString *number;
@property (nonatomic, retain) NSString *songListNumber;

@end

@implementation XCMainViewController


-(void)dealloc{
    
    self.findTableView.dataSource = nil;
    self.findTableView.delegate = nil;
    self.scrollView.delegate = nil;
    self.imagePicker.delegate = nil;
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    self.myTableView.delegate = nil;
    self.myTableView.dataSource = nil;
    
    [_dataSource release];
    [_findTableView release];
    [_autoScrollView release];
    [_seg release];
    [_firstDataModel release];
    [_picArray release];
    [_scrollView release];
    [_senderPicUrl release];
    [_myTableView release];
    [_songlistNameMutableArray release];
    [_mutableArraySingerNAme release];
    [_mutableArraySongName release];
    [_nameArray release];
    [_tuiJianDataSource release];
    [_number release];
    [_songListNumber release];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//    [center addObserver:self selector:@selector(receivesNotiFication:) name:@"image" object:nil];
    
    if (self.libraryPath) {
        _myHeadImageView.image = [NSKeyedUnarchiver unarchiveObjectWithFile:self.libraryPath];
    } else {
        
        _myHeadImageView.image = [[UIImage imageNamed:@"Image-myHeadImageView"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    if ([OnlineAVplay shareAVPlayer].isPlay == NO) {
        [XCSingleBottumView shareBottomViewBuild].hidden = YES;
    } else {
        [XCSingleBottumView shareBottomViewBuild].hidden = NO;
    }
    
//    WDGDatabaseTool *myLove1 = [WDGDatabaseTool DBManageWithTableName:@"myLove"];
//    [myLove1 openDatabase];
//    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:[myLove1 selectAllData]];
//    self.songListNumber = [NSString stringWithFormat:@"%lu", mutableArray.count];
//    [myLove1 closeDatabase];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.number = [[NSUserDefaults standardUserDefaults] objectForKey:@"number"];
    [XCSingleBottumView shareBottomViewBuild].delegate = self;
    self.tuiJianDataSource = [NSMutableArray arrayWithCapacity:0];
//    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
    self.navigationController.navigationBar.translucent = NO;
    self.songlistNameMutableArray = [NSMutableArray arrayWithObjects:@"我的最爱", nil];
    
#pragma mark - 视图的搭建
//    导航栏
    [self navigationControllerBeginBuild];
//    中间3个按钮
    [self segmentedControlBeginBuild];
//    创建 uiscrollView
    [self scrollViewBeginBuild];
//    创建 tableView
    [self findingtableViewBuild];
    
    [self createHeadView];
//    创建底部视图
#pragma mark - mytableViewBuild
    [self myTableViewBuild];
#pragma mark - 调用创建方法
    [self collectionViewBuild];
#pragma mark - 推荐网络请求
    [self request];
    [self TuiJianNetRequest];

}

- (void)createHeadView{
//    头视图
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, wide / 2)];

self.autoScrollView = [[AutoScrollView alloc] initWithFrame:CGRectMake(0, 0, wide, wide / 2)];
[headView addSubview:self.autoScrollView];
_findTableView.tableHeaderView = headView;
    [headView release];
    [_autoScrollView release];
    
    /// 点击图片跳转
    void (^blockLxc)(NSInteger) =^(NSInteger index){
        
        
        XCFirstDataModel *firstDataModel = self.dataSource[0];
        
        if (index == 0 || index == 5) {
            XCSecDataModel *secModel = firstDataModel.data[4];
            NSString *value = secModel.action[@"value"];
            if (value.length > 10) {
                XCWebViewViewController *webVVC = [[XCWebViewViewController alloc] initWithString:value];
                [self.navigationController pushViewController:webVVC animated:YES];
            } else {
                XCHotSongListViewController *hotSongVC = [[XCHotSongListViewController alloc] initWithValue:value WithSection:2];
                [self.navigationController pushViewController:hotSongVC animated:YES];
            }
            
        } else {
            XCSecDataModel *secModel = firstDataModel.data[index - 1];
            NSString *value = secModel.action[@"value"];
            if (value.length > 10) {
                XCWebViewViewController *webVVC = [[XCWebViewViewController alloc] initWithString:value];
                [self.navigationController pushViewController:webVVC animated:YES];
            } else {
                XCHotSongListViewController *hotSongVC = [[XCHotSongListViewController alloc] initWithValue:value WithSection:2];
                [self.navigationController pushViewController:hotSongVC animated:YES];
            }
        }
    };
    self.autoScrollView.block = blockLxc;
}
#pragma mark - 网络请求发现页面
- (void)request {
    
    [XCAFNetTool GETNetWithUrl:[XCUrlManger UrlRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {
        self.dataSource = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            
                        self.firstDataModel = [[XCFirstDataModel alloc] initWithDataSource:dic];
            
                        [self.dataSource addObject:self.firstDataModel];
            
                        [_firstDataModel release];
                    }
            //        头视图数组(picUrl)
                    self.picArray = [NSMutableArray arrayWithCapacity:0];
                    if (self.picArray) {
                        self.autoScrollView.picMutableArray = self.picArray;
                    }
                    XCFirstDataModel *firstModel = self.dataSource[0];
                    for (XCSecDataModel *secModel in firstModel.data) {
                        [self.picArray addObject:secModel.picUrl];
            
                    }
            
                    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
                    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
                    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
                    self.nameArray = [NSMutableArray arrayWithCapacity:0];
                    XCFirstDataModel *firstModel1 = self.dataSource[2];
                    XCSecDataModel *secDataModel = firstModel1.data[0];
            
                    for (XCSongsModel *songsModel in secDataModel.songs) {
                        [self.mutableArraySingerNAme addObject:songsModel.singerName];
                        [self.mutableArraySongName addObject:[songsModel.urlList[0] objectForKey:@"url"]];
                        [self.mutableHeadIVPicurl addObject:songsModel.picUrl];
                        [self.nameArray addObject:songsModel.name];
                    }
        
                    self.autoScrollView.picMutableArray = self.picArray;
                    [self.autoScrollView scrollViewBuild];
                    [self.autoScrollView pageControllBuild];
        
    
                    if ([self.dataSource count] > 0) {
                        [self.findTableView reloadData];
                    }
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
#pragma mark - 网络请求推荐页面
- (void)TuiJianNetRequest {
    
    [XCAFNetTool GETNetWithUrl:[XCUrlManger TuiJianNetRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            
            XCTuiJianModel *tuiJianModel = [[XCTuiJianModel alloc] initWithDataSource:dic];

            [self.tuiJianDataSource addObject:tuiJianModel];
            [tuiJianModel release];
        }
        if ([self.tuiJianDataSource count] > 0) {
            [self.collectionView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
#pragma mark - scrollViewBuild
- (void)scrollViewBeginBuild {
    //    创建 uiscrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, wide, high)];
    _scrollView.contentSize = CGSizeMake(wide * 3, 0);
    _scrollView.contentOffset = CGPointMake(wide * 2, 0);
    _scrollView.bounces = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = YES;
    
    [self.view addSubview:_scrollView];
    [_scrollView release];
}
#pragma mark - scrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger index = _scrollView.contentOffset.x / wide;
    _seg.selectedSegmentIndex = index;
    
}
#pragma mark - 导航栏的设计
- (void)navigationControllerBeginBuild {
    //    导航栏
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.2095 green:0.8778 blue:0.9941 alpha:1.0];
    
    //    左侧按钮
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-edit"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)]autorelease];
    //    右侧按钮
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction)] autorelease];
    
    self.navigationController.navigationBar.translucent = NO;
}
#pragma mark - segmentedControl
- (void)segmentedControlBeginBuild {
    //    中间3个按钮
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"我的", @"推荐", @"发现"]];
    
    
    self.seg.frame = CGRectMake(0, 20, wide / 2, 50);
    _seg.selectedSegmentIndex = 2;
    self.navigationItem.titleView = _seg;
    //    去边框
    _seg.tintColor = [UIColor clearColor];
    
    NSDictionary *dicOne = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
    
    [_seg setTitleTextAttributes:dicOne forState:UIControlStateNormal];
    NSDictionary *dicTwo = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor colorWithRed:0.91 green:1.0 blue:0.9668 alpha:1.0]};
    
    [_seg setTitleTextAttributes:dicTwo forState:UIControlStateSelected];
    //    添加点击事件
    [_seg addTarget:self action:@selector(controllPress:) forControlEvents:UIControlEventValueChanged];
    
    [_seg release];
}
#pragma mark - 监听 UISegmentController
- (void)controllPress:(UISegmentedControl *) segControll {
    
    NSInteger index = _seg.selectedSegmentIndex;
    self.scrollView.contentOffset = CGPointMake(wide * index, 0);
    
}
#pragma mark - 编辑点击事件(抽屉)
- (void)leftBarButtonItemAction:(id) sender {
    NSString *stringFloderSize = [CleanCaches folderSizeAtPath];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"stringFloderSize" object:stringFloderSize userInfo:nil];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}
#pragma mark - 查找点击事件
- (void)rightBarButtonItemAction {
    XCSearchBarViewController *searchBarVC = [[XCSearchBarViewController alloc] init];
    [self.navigationController pushViewController:searchBarVC animated:YES];
    [searchBarVC release];
}
#pragma mark - playView创建
- (void)playViewBuild {

    [XCPlayViewController sharePlayViewController].imgViewPicUrl = self.senderPicUrl;
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
        
    }];
    }
#pragma mark - collection 推荐页面
- (void)collectionViewBuild {
    HJCarouselViewLayout *layout = [[HJCarouselViewLayout alloc] initWithAnim:HJCarouselAnimCarousel];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(self.view.bounds.size.width - 50, self.view.bounds.size.width - 50);

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(wide, 0, wide, high)collectionViewLayout:layout];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[XCTuiJianCollectionViewCell class] forCellWithReuseIdentifier:@"XCTuiJianCollectionViewCell"];
    
    [_scrollView addSubview:self.collectionView];
    
}
#pragma mark - 我的界面实现
- (void)myTableViewBuild {
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high) style:UITableViewStylePlain];
//    self.myTableView.tag = 1001;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.myTableView.backgroundColor = [UIColor clearColor];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
//  下载 播放 本地 MV
    [self.myTableView registerClass:[XCMyLocalPlayDownLoadTableViewCell class] forCellReuseIdentifier:@"XCMyLocalPlayDownLoadTableViewCell"];
    
    [self.myTableView registerClass:[XCMyCollectionTableViewCell class] forCellReuseIdentifier:@"XCMyCollectionTableViewCell"];
    
    [self.myTableView registerClass:[XCCollectionSonglistTableViewCell class] forCellReuseIdentifier:@"XCCollectionSonglistTableViewCell"];
    /*****************************************************/

//    头视图
    UIView *myHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, self.myTableView.bounds.size.height / 2)];
    self.myHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -100, wide, high / 2+70)];
    _myHeadImageView.userInteractionEnabled = YES;
    /////////////////////////////////////////
    if (self.libraryPath) {
        _myHeadImageView.image = [NSKeyedUnarchiver unarchiveObjectWithFile:self.libraryPath];
    } else {
        
    _myHeadImageView.image = [[UIImage imageNamed:@"Image-myHeadImageView"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receivesNotiFication:) name:@"image" object:nil];
    ////////////////////////////////////////
//    给 imageView 加 tap 手势
    UITapGestureRecognizer *tapChangeImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionChangeImage)];
    tapChangeImage.numberOfTapsRequired = 1;
    tapChangeImage.numberOfTouchesRequired = 1;
    [_myHeadImageView addGestureRecognizer:tapChangeImage];
    
    
    [myHeadView addSubview:_myHeadImageView];
    self.myTableView.tableHeaderView = myHeadView;
    
    [_scrollView addSubview:_myTableView];
    [myHeadView release];
    [_myHeadImageView release];
    [_myTableView release];
}
#pragma mark - 通知接收事件 
- (void)receivesNotiFication:(NSNotification *) libraryPath {
    self.libraryPath = libraryPath.object;
    
    if (libraryPath.object) {
        
        _myHeadImageView.image = [NSKeyedUnarchiver unarchiveObjectWithFile:libraryPath.object];
        
        
    } else {
        _myHeadImageView.image = [[UIImage imageNamed:@"Image-myHeadImageView"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}
#pragma mark - tableViewBuild(发现界面)
- (void)findingtableViewBuild {
    // 创建 tableView
    _findTableView = [[UITableView alloc] initWithFrame:CGRectMake(wide * 2, 0, wide, high) style:UITableViewStylePlain];
//    _findTableView.tag = 1000;
    _findTableView.dataSource = self;
    _findTableView.delegate = self;
    _findTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _findTableView.backgroundColor = [UIColor clearColor];
/***********************************************************/
//    重用池的注册
//    图标
    [_findTableView registerClass:[XCIconTableViewCell class] forCellReuseIdentifier:@"XCIconTableViewCellIdentifier"];
//   分享音乐
    [_findTableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCellIdentifier"];
//    热门歌单datasource[4]
    [_findTableView registerClass:[XCHotListTableViewCell class] forCellReuseIdentifier:@"XCHotListTableViewCellIdentifier"];
//    我是歌手 dataSource[5]
    [_findTableView registerClass:[XCIMSingerTableViewCell class] forCellReuseIdentifier:@"XCIMSingerTableViewCellIdentifier"];
//    亚洲
    [_findTableView registerClass:[XCAsiaNSTableViewCell class] forCellReuseIdentifier:@"XCAsiaNSTableViewCellIdentifier"];
    
    [_scrollView addSubview:_findTableView];
}
#pragma mark - tableView 的协议方法
// section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == _findTableView) {
        if (self.dataSource) {
            return self.dataSource.count - 1;
        }
        return 0;
    } else if (tableView == _myTableView) {
        return 1;
    } else {
        return 0;
    }
    
}
///  section头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == _findTableView) {
        if (section == 0) {
            
            return nil;
            
        } else if (section == 1) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, wide / 8 - 16)];
//            view.backgroundColor = [UIColor whiteColor];
            
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, wide, 21)];
            lable.text = @"天籁之音, 与您共分享";
            
            UIButton *MoreButton = [UIButton buttonWithType:UIButtonTypeSystem];
            MoreButton.frame = CGRectMake(wide * 4 / 5 + 50, 5, 21, 21);
            [MoreButton setImage:[[UIImage imageNamed:@"Image-more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [MoreButton addTarget:self action:@selector(shareMusicMoreButton) forControlEvents:UIControlEventTouchUpInside];
            
            
            [view addSubview:MoreButton];
            [view addSubview:lable];
            [lable release];
            return [view autorelease];
        } else if (section == 2 || section == 3 || section == 4 || section == 7 || section == 8) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, wide / 8 - 16)];
//            view.backgroundColor = [UIColor whiteColor];
            
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, wide, 21)];
            XCFirstDataModel *firstModel = self.dataSource[section + 1];
            lable.text = firstModel.name;
            
            [view addSubview:lable];
            [lable release];
            
            return [view autorelease];
            
        } else  {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, wide / 8 - 16)];
//            view.backgroundColor = [UIColor whiteColor];
            
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, wide, 21)];
            XCFirstDataModel *firstModel = self.dataSource[section + 1];
            lable.text = firstModel.name;
//            UIButton *MoreButton = [UIButton buttonWithType:UIButtonTypeSystem];
            
//            MoreButton.frame = CGRectMake(wide * 4 / 5 + 50, 5, 21, 21);
//            [MoreButton setImage:[[UIImage imageNamed:@"Image-more"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            
//            [view addSubview:MoreButton];
            [view addSubview:lable];
            [lable release];
            return [view autorelease];
            
        }
        
        return nil;
    } else if (tableView == _myTableView) {
        return nil;
    } else {
        return nil;
    }
    
}
/// section尾视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (tableView == _findTableView) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, 1)];
        view.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
        
        return view;
    }
    return nil;

}
/// 返回头视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == _findTableView) {
        if (section == 0) {
            return 0;
        }
        return wide / 12;
    } else if (tableView == _myTableView) {
        return 0;
    } else {
        return 0;
    }
    
}
/// 返回尾视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (tableView == _findTableView) {
        return 1;
    }
    return 0;
    
}
/// 返回 tableView 的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _findTableView) {
        if (section == 0) {
            return 1;
        } else if (section == 1) {
            self.firstDataModel = self.dataSource[section+1];
            XCSecDataModel *secModel = self.firstDataModel.data[0];
            return secModel.songs.count;
        } else if (section == 2) {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count / 3;
        } else if (section == 3) {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count / 3;
            
        } else if (section == 4) {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count;
        }
        else if (section == 5) {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count / 3;
        } else if (section == 6) {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count / 3;
            
        } else if (section == 7) {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count ;
        } else  {
            self.firstDataModel = self.dataSource[section+1];
            return self.firstDataModel.data.count;
        }
    } else if (tableView == _myTableView) {
        
        return self.songlistNameMutableArray.count + 2;
    }
        return 0;
    

}
/// 返回 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _findTableView) {
        if (indexPath.section == 0) {
            XCIconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCIconTableViewCellIdentifier"];
            //        排行榜的 button
            [cell.rankButton addTarget:self action:@selector(rankButtonAction:) forControlEvents:UIControlEventTouchUpInside];
           //          歌单
            [cell.songListButton addTarget:self action:@selector(songListButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            //        电台的 button
            [cell.radioButton addTarget:self action:@selector(radioButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            //        歌手的 button
            [cell.singerButton addTarget:self action:@selector(singerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
            
        } else if (indexPath.section == 1) {
            
            XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCellIdentifier"];
            XCFirstDataModel *firstModel = self.dataSource[indexPath.section + 1];
            XCSecDataModel *secModel = firstModel.data[0];
            XCSongsModel *songsModel = secModel.songs[indexPath.row];
            
            cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            cell.songsModel = songsModel;
            
            
            return cell;
            
        } else if (indexPath.section == 2) {
            
            XCHotListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCHotListTableViewCellIdentifier"];
            cell.row = indexPath.row;
            cell.section = 2;
            cell.delegate = self;
            XCFirstDataModel *firstModel = self.dataSource[indexPath.section + 1];
            
            //      有3个 collection cell 为一个tableViewcell
            //        有六个需要两行所以一行一行传;
            
            NSMutableArray *marray=[NSMutableArray arrayWithCapacity:0];
            
            for (NSInteger i = indexPath.row * 3; i < (indexPath.row+1) * 3; i++) {
                [marray addObject:firstModel.data[i]];
            }
            cell.mutableArray = marray;
            return cell;
            
        } else if (indexPath.section == 3) {
            
            XCHotListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCHotListTableViewCellIdentifier"];
            XCFirstDataModel *firstModel = self.dataSource[indexPath.section + 1];
            cell.mutableArray = [NSMutableArray arrayWithArray:firstModel.data];
            if (indexPath.row > 2) {
              cell.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            } else {
              cell.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
            }
            cell.row = indexPath.row;
            cell.section = 3;
            cell.delegate = self;
            return cell;
            
        } else if (indexPath.section == 4) {
            
            XCIMSingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCIMSingerTableViewCellIdentifier"];
            XCFirstDataModel *firstModle = self.dataSource[indexPath.section + 1];
            cell.secModel = firstModle.data[indexPath.row];
            return cell;
            
        } else if (indexPath.section == 5) {
            
            XCHotListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCHotListTableViewCellIdentifier"];
            XCFirstDataModel *firstModel = self.dataSource[indexPath.section + 1];
            cell.mutableArray = [NSMutableArray arrayWithArray:firstModel.data];
//          嵌套传了两个参数:一个是第几行, 还有一个是第几个区
            cell.row = indexPath.row;
            cell.section = 5;
            cell.delegate = self;
            return cell;
            
        } else if (indexPath.section == 6) {
            
            XCHotListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCHotListTableViewCellIdentifier"];
            XCFirstDataModel *firstModel = self.dataSource[indexPath.section + 1];
            cell.mutableArray = [NSMutableArray arrayWithArray:firstModel.data];
//
            cell.row = indexPath.row;
            cell.section = 6;
//
            cell.delegate = self;
            return cell;
            
        } else if (indexPath.section == 7) {
            
            XCIMSingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCIMSingerTableViewCellIdentifier"];
            XCFirstDataModel *firstModle = self.dataSource[indexPath.section + 1];
            cell.secModel = firstModle.data[indexPath.row];
            return cell;
            
        } else {
            
            XCAsiaNSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCAsiaNSTableViewCellIdentifier"];
            XCFirstDataModel *firstModle = self.dataSource[indexPath.section + 1];
            cell.secModel = firstModle.data[indexPath.row];
            return cell;
            
        }
        
    } else  {
        if (indexPath.row == 0) {
            
            XCMyLocalPlayDownLoadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCMyLocalPlayDownLoadTableViewCell"];
            
            [cell.localButton addTarget:self action:@selector(localButtonAction) forControlEvents:UIControlEventTouchUpInside];
//            [cell.MVDownLoadButton addTarget:self action:@selector(MVDownLoadAction) forControlEvents:UIControlEventTouchUpInside];
//            [cell.recentPlayButton addTarget:self action:@selector(recentPlayAction) forControlEvents:UIControlEventTouchUpInside];
            [cell.musicDownLoadButton addTarget:self action:@selector(musicDownLoadAction) forControlEvents:UIControlEventTouchUpInside];
            
            return cell;
        }  else if (indexPath.row == 1) {
            XCMyCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCMyCollectionTableViewCell"];
            cell.myCollectionLable.text = @"我的歌单";
            
            return cell;
        }
        
        XCCollectionSonglistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCCollectionSonglistTableViewCell"];
//        cell.collectionNameLable.text = self.songlistNameMutableArray[indexPath.row - 2];
        
//        cell.numberLable.text = self.songListNumber;

        
        return cell;
    }
    
}
/// 返回 cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _findTableView) {
        if (indexPath.section == 0) {
            
            return wide / 4 - 30;
            
        } else if (indexPath.section == 1) {
            
            return wide / 5 ;
            
        } else if (indexPath.section == 2) {
            
            return wide / 3 + 42 ;
            
        } else if (indexPath.section == 3) {

            return wide / 3 + 42 ;

        } else if (indexPath.section == 4) {
            
            return wide / 3 ;
            
        } else if (indexPath.section == 5) {
            
            return wide / 3 + 42 ;
            
        } else if (indexPath.section == 6) {

               return wide / 3 + 42 ;

        } else if (indexPath.section == 7) {
             
            return wide / 3 ;
            
        }
        
        return wide / 3;
    } else if (tableView == _myTableView){
        if (indexPath.row == 0) {
            return wide / 4;
        }else if (indexPath.row == 1){
            return 21;
        } else if (indexPath .row == 2){
            return wide / 8 + 10;
        } else {
            return wide / 8 + 10;
        }
    } else {
        return 0;
    }
    
}
#pragma mark - cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    if (tableView == _findTableView) {

        if (indexPath.section == 4 || indexPath.section == 7 || indexPath.section == 8) {
            XCFirstDataModel *firstDataModel = self.dataSource[indexPath.section + 1];
            XCSecDataModel *secModel = firstDataModel.mutableArray[indexPath.row];
            
            XCWebViewViewController *webVVC = [[XCWebViewViewController alloc] initWithString:[secModel.action objectForKey:@"value"]];
            [self.navigationController pushViewController:webVVC animated:YES];
            
            [webVVC release];
        } else if (indexPath.section == 1) {
            
            [OnlineAVplay shareAVPlayer].isPlay = YES;

            [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
            [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
            [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
            [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
            
            [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
            
            XCFirstDataModel *firstDataModel1 = self.dataSource[indexPath.section + 1];
            
            XCSecDataModel *secModel = firstDataModel1.data[0];
            
            XCSongsModel *songModel = secModel.songs[indexPath.row];
            
            [[OnlineAVplay shareAVPlayer] playWithUrl:[songModel.urlList[0] objectForKey:@"url"]];
            
            [[OnlineAVplay shareAVPlayer] play];
            
            [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
                
                
            }];
            
        }
    } else if (tableView == _myTableView){
        
        if (indexPath.row == 2) {
                XCMySongListViewController *mySongListVC = [[XCMySongListViewController alloc] init];
                [self.navigationController pushViewController:mySongListVC animated:YES];
            
         }
       
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];


}
#pragma mark - collection的代理
- (NSIndexPath *)curIndexPath {
    NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *curIndexPath = nil;
    NSInteger curzIndex = 0;
    for (NSIndexPath *path in indexPaths.objectEnumerator) {
        UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:path];
        if (!curIndexPath) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
            continue;
        }
        if (attributes.zIndex > curzIndex) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
        }
    }
    return curIndexPath;
}
// 是否应该选择 Item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (indexPath.row == curIndexPath.row) {
        return YES;
    }
    
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    return NO;
}
// 点击 collection 的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XCTuiJianModel *tuiJianModel = self.tuiJianDataSource[indexPath.row];
    
    XCTuiJianDetialViewController *tuiJianDetailVC = [[XCTuiJianDetialViewController alloc] initwithAlbumId:[NSString stringWithFormat:@"%@", tuiJianModel.albumId]];
    [self.navigationController pushViewController:tuiJianDetailVC animated:YES];
    
//    NSLog(@"click %ld", indexPath.row);
}
// 返回 cell 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.tuiJianDataSource.count;
}
// 返回 collectioncell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XCTuiJianCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XCTuiJianCollectionViewCell" forIndexPath:indexPath];

    XCTuiJianModel *tuiJianModel = self.tuiJianDataSource[indexPath.row];

    cell.tuiJianModel = tuiJianModel;
    
    return cell;
}
#pragma mark - 第一个 cell 中的四个 button 点击事件
- (void)rankButtonAction:(id) sender {
    
    XCRankViewController *rankVC = [[XCRankViewController alloc] init];
    [self.navigationController pushViewController:rankVC animated:YES];
    [rankVC release];
}
- (void)radioButtonAction:(id) sender {
    XCRadioButtonViewController *radioVC = [[XCRadioButtonViewController alloc] init];
    [self.navigationController pushViewController:radioVC animated:YES];
    [radioVC release];
}
- (void)singerButtonAction:(id) sender {
    XCSingerButtonViewController *singerVC = [[XCSingerButtonViewController alloc] init];
    [self.navigationController pushViewController:singerVC animated:YES];
    [singerVC release];
}
- (void)songListButtonAction:(id) sender {
    
    XCSongListButtonViewController *songListButtonVC = [[XCSongListButtonViewController alloc] init];
    [self.navigationController pushViewController:songListButtonVC animated:YES];
    [songListButtonVC release];
    
}
#pragma mark - 放大和缩小 myHeadImageView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.myHeadImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.myHeadImageView.transform = CGAffineTransformMakeScale(scale, scale);
}
#pragma mark - 更换壁纸
- (void)tapActionChangeImage {
    self.imagePicker = [[UIImagePickerController alloc] init];
//    设置图片来源
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = YES;
    [self presentViewController:self.imagePicker animated:YES completion:^{
        
    }];
    [_imagePicker release];
}
// 从相册取照片的协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    self.myHeadImageView.image = image;
}
#pragma mark - 本地音乐, 歌曲下载, MV 下载, 最近播放
- (void)localButtonAction {
//    NSLog(@"本地音乐");
    CATransition *animation = [CATransition animation];
    animation.duration = 1.5f;
    [animation setType:@"rippleEffect"];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    XCMyProjectViewController *myProjectVC = [[XCMyProjectViewController alloc] init];
   
    [self.navigationController pushViewController:myProjectVC animated:YES];
}
- (void)musicDownLoadAction {
    XCRecentPlayViewController *recentPlayVC = [[XCRecentPlayViewController alloc] init];
    
    [self.navigationController pushViewController:recentPlayVC animated:YES];
    
    [recentPlayVC release];

}
//- (void)MVDownLoadAction {
//    NSLog(@"MV 下载");
//}
//- (void)recentPlayAction {
//    NSLog(@"最近播放");
//}
#pragma 播放界面传过来的是否正在播放(协议传值)
- (void)sendIsPlaying:(BOOL)isPlaying {
    
  [OnlineAVplay shareAVPlayer].isPlay = isPlaying;

}
#pragma mark - shareMusicMoreButtonAction
- (void)shareMusicMoreButton {
    
    XCShareMusicViewController *shareMusicVC = [[XCShareMusicViewController alloc] init];
    [self.navigationController pushViewController:shareMusicVC animated:YES];
    
}
#pragma mark - 嵌套的 collection 传值
- (void)sendWithIndex:(NSInteger)index WithRow:(NSInteger)row WithSection:(NSInteger)section{
    
    if (section == 2) {
        
        XCFirstDataModel *firstDataModel1 = self.dataSource[section + 1];
        if (row == 0) {
            XCSecDataModel *secModel = firstDataModel1.data[index];
            XCHotSongListViewController *hotSongListVC = [[XCHotSongListViewController alloc] initWithValue:[secModel.action objectForKey:@"value"] WithSection:section];
            [self.navigationController pushViewController:hotSongListVC animated:YES];
        } else {
            XCSecDataModel *secModel = firstDataModel1.data[index + 3];
            XCHotSongListViewController *hotSongListVC = [[XCHotSongListViewController alloc] initWithValue:[secModel.action objectForKey:@"value"] WithSection:section];
            [self.navigationController pushViewController:hotSongListVC animated:YES];
        }
    } else if (section == 3) {
        XCFirstDataModel *firstDataModel1 = self.dataSource[section + 1];
            XCSecDataModel *secModel = firstDataModel1.data[index];
            XCHotSongListViewController *hotSongListVC = [[XCHotSongListViewController alloc] initWithValue:[secModel.action objectForKey:@"value"] WithSection:section];
            [self.navigationController pushViewController:hotSongListVC animated:YES];
    } else if (section == 5) {
        XCFirstDataModel *firstDataModel1 = self.dataSource[section + 1];
        
        XCSecDataModel *secModel = firstDataModel1.data[index];
        
        XCHotSongListViewController *hotSongListVC = [[XCHotSongListViewController alloc] initWithValue:[secModel.action objectForKey:@"value"] WithSection:section];
        NSLog(@"%@", [secModel.action objectForKey:@"value"]);
        NSLog(@"%ld", (long)section);
        [self.navigationController pushViewController:hotSongListVC animated:YES];
    } else if (section == 6) {
        XCFirstDataModel *firstDataModel1 = self.dataSource[section + 1];
        XCSecDataModel *secModel = firstDataModel1.data[index];
        
        XCNewMVViewController *newMVVC = [[XCNewMVViewController alloc] initWithValue:[secModel.action objectForKey:@"value"]WithIndex:index];
//        [self.navigationController pushViewController:newMVVC animated:YES];
        [self.navigationController presentViewController:newMVVC animated:YES completion:^{
            
        }];
    }
    

}
#pragma mark - 执行跳转到 playView 的协议方法
- (void)sendpresentPlayView {
    
  [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
      
  }];
    
    
    
}
#pragma mark - 执行 buttonListAction "
- (void)sendButtonListAction {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"歌曲质量" message:@"本版本只支持流畅版本" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"流畅" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"标准" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sure2 = [UIAlertAction actionWithTitle:@"高清" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:sure];
    [alert addAction:sure1];
    [alert addAction:sure2];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
